package com.yanq.liancar.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;


import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxPermissionsTool;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.ProtocolListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.databinding.ActivitySelectBinding;
import com.yanq.liancar.net.Net;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

public class SelectActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, false, true);
        ActivitySelectBinding bindingView = DataBindingUtil.setContentView(this, R.layout.activity_select);
        bindingView.tvFuwu.setOnClickListener(this);
        bindingView.tvYonghu.setOnClickListener(this);
        initPermission();

        if (!TextUtils.isEmpty(UserBean.getcToken(this))){
//            if (UserBean.getRoleId(this)==1){         //去小C
//                Intent intent=new Intent(this, MinUserMainActivity.class);
//                //清空activity栈
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("type",3);
//                startActivity(intent);
//            }else if (UserBean.getRoleId(this)==2){   //去大C
//                Intent intent=new Intent(this, CMainActivity.class);
//                //清空activity栈
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
//            }
//            finish();
            Intent intent2 = new Intent(this, MinUserMainActivity.class);
            startActivity(intent2);
            RxSPTool.putInt(this, "LoginType",3);
            finish();
        }else if (!TextUtils.isEmpty(UserBean.getbToken(this))){
            LogUtils.e("adadadad",UserBean.getbAgreement(this)+"");
            if (UserBean.getbAgreement(this)!=0){
                Bundle bundle=new Bundle();
                bundle.putInt("type",1);
                RxActivityTool.skipActivity(this, ProtocolListActivity.class,bundle);
            }else {
                Intent intent = new Intent(this, ServerMainActivity.class);
                startActivity(intent);
                RxSPTool.putInt(this, "LoginType",1);
                finish();
            }
        }

        verifyStoragePermissions(SelectActivity.this);
        Net.checkVersion(SelectActivity.this, false);
    }

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static final int INSTALL_PACKAGES_REQUESTCODE = 2;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

    private void putPushToken() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_fuwu:
                if ("".equals(UserBean.getbToken(this))|| TextUtils.isEmpty(UserBean.getbToken(this))){
                    Intent intent = new Intent(this, BLoginActivity.class);
                    startActivity(intent);
//                    RxSPTool.putInt(this, "LoginType",1);
//                    finish();
                }else {
                    if (UserBean.getbAgreement(this)!=0){
                        RxActivityTool.skipActivity(this, ProtocolListActivity.class);
                    }else {
                        Intent intent = new Intent(this, ServerMainActivity.class);
                        startActivity(intent);
                        RxSPTool.putInt(this, "LoginType",1);
                        finish();
                    }
                }

                break;
            case R.id.tv_yonghu:
                Intent intent2 = new Intent(this, MinUserMainActivity.class);
                startActivity(intent2);
                RxSPTool.putInt(this, "LoginType",3);
                finish();
                break;
        }
    }
    private void initPermission() {
        RxPermissionsTool.with(this)
                .addPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
                .addPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                .addPermission(Manifest.permission.ACCESS_NETWORK_STATE)
                .addPermission(Manifest.permission.ACCESS_WIFI_STATE)
                .addPermission(Manifest.permission.CHANGE_WIFI_STATE)
                .addPermission(Manifest.permission.INTERNET)
                .addPermission(Manifest.permission.READ_PHONE_STATE)
                .addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .addPermission(Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS)
                .addPermission(Manifest.permission.BLUETOOTH)
                .addPermission(Manifest.permission.BLUETOOTH_ADMIN)
                .addPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .addPermission(Manifest.permission.CAMERA)
                .addPermission(Manifest.permission.CALL_PHONE)
                .initPermission();
    }


}
